<?php
include_once 'common/tools.php';

$key = $_GET["key"];
$page = $_GET["page"];
$keySimp = $_GET["keySimp"];
$locale = $_GET["locale"];

$config = require 'conf/config.ini.php'; //加载公用config.ini.php

$conn = mysql_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"]);
mysql_select_db($config["DB_NAME"], $conn);
mysql_query("SET NAMES utf8");


$sql = "select count(*) count from (" .
	"select 1 " .
	"FROM live_match m	LEFT JOIN view_team_name v on (m.homeTeam = v.name_cn or m.visitTeam = v.name_cn)".
	"					LEFT JOIN live_watch w ON m.matchId = w.matchId ".
	"WHERE " .
	"	m.matchName like '%" . $keySimp . "%' " .
	"or	m.homeTeam like '%" . $key . "%' " .
	"or m.visitTeam like '%" . $key . "%' " .
	"or v.name_en like '%" . $key . "%' " .
	"or v.name_tw like '%" . $key . "%' ".
	"GROUP BY m.matchId ".
	")matchs";

$rs = mysql_fetch_array(mysql_query($sql, $conn));
$total = $rs["count"];

if ($total > 0) {
	$sql = "select " .
	"	   m.matchId," .
	"	   m.matchName," .
	"	   m.matchTime," .
	"	   m.matchCategory," .
	"	   m.matchStatus," .
	"	   m.matchRecord," .
	"	   m.matchOptaId," .
	"	   m.matchEvent,".
	"	   IFNULL((select ";
	if (strpos($locale, "EN")) {
		$sql .= "	   name_en ";
	} else if (strpos($locale, "TW") || strpos($locale, "HK")) {
		$sql .= "	   name_tw ";
	} else {
		$sql .= "	   name ";
	}
	$sql .= "from (select name,name_en,name_tw from live_team_b UNION ALL select name,name_en,name_tw from live_team_f) t" .
	"		 where t.name = m.visitTeam" .
	"	   ),m.visitTeam) visitTeam," .
	"	   m.visitTeam visitTeamName," .
	"	   m.visitTeamPic," .
	"	   m.visitTeamScore," .
	"	   IFNULL((select ";
	if (strpos($locale, "EN")) {
		$sql .= "	   name_en ";
	} else if (strpos($locale, "TW") || strpos($locale, "HK")) {
		$sql .= "	   name_tw ";
	} else {
		$sql .= "	   name ";
	}
	$sql .= "from (select name,name_en,name_tw from live_team_b UNION ALL select name,name_en,name_tw from live_team_f) t" .
	"		 where t.name = m.homeTeam" .
	"	   ),m.homeTeam) homeTeam," .
	"	   m.homeTeam homeTeamName," .
	"	   m.homeTeamPic," .
	"	   m.homeTeamScore," .
	"	   COUNT(DISTINCT CONCAT(w.userIp, w.userMac)) matchWatch ".
	"	   FROM live_match m	LEFT JOIN view_team_name v on (m.homeTeam = v.name_cn or m.visitTeam = v.name_cn)".
	"							LEFT JOIN live_watch w ON m.matchId = w.matchId ".
	"WHERE " .
	"	m.matchName like '%" . $keySimp . "%' " .
	"or	m.homeTeam like '%" . $key . "%' " .
	"or m.visitTeam like '%" . $key . "%' " .
	"or v.name_en like '%" . $key . "%' " .
	"or v.name_tw like '%" . $key . "%' " .
	"GROUP BY m.matchId " .
	"ORDER BY m.matchTime DESC " .
	"limit " . $page * 10 . ", 10";

	$matchList = mysql_query($sql, $conn);
	while ($match = mysql_fetch_object($matchList)) {
		$matchArray[] = $match;
	}

	//启用一个带有ob_gzip压缩机的工作台
	ob_start('ob_gzip');

	echo '{"sql":"' . $sql . '","result":"Y","results":' . json_encode($matchArray) . ',"total":"' . $total . '"}';

	//输出压缩成果
	ob_end_flush();


} else {
	//启用一个带有ob_gzip压缩机的工作台
	ob_start('ob_gzip');

	echo '{"sql":"' . $sql . '","result":"N","total":0}';

	//输出压缩成果
	ob_end_flush();
}
?>
